<?php

namespace tmcore\services\crud\generators;

use tmcore\services\crud\enum\TplEnum;
use tmcore\services\crud\UtilService;

class Validate extends Generate
{

    /**
     * @var string
     */
    protected string $fileType = 'php';

    /**
     * 替换变量
     */
    public function replaceContent()
    {
        $search = [
            '{NAMESPACE}',
            '{TABLE_COMMENT}',
            '{CAMEL_CLASS_NAME}',
            '{RULE}',
            '{MESSAGE}',
            '{CONTENT_PHP}'
        ];

        $replace = [
            $this->getNameSpaceContent(),
            $this->getTableCommentContent(),
            $this->getCamelClassName(),
            $this->getRuleContent(),
            $this->getMessageContent(),
            $this->getValidateContent()
        ];

        $content = UtilService::replaceFileContent($search, $replace, $this->getStub('validate'));

        $this->setContent($content);
    }

    /**
     * 返回模板路径
     * @param string $type
     * @return mixed|string|string[]
     */
    public function getStub(string $type = 'validate')
    {
        $basePath = 'php/validate/';

        $stubs = [
            'add' => $this->getTplPath($basePath . 'add'),
            'edit' => $this->getTplPath($basePath . 'edit'),
            'delete' => $this->getTplPath($basePath . 'delete'),
            'detail' => $this->getTplPath($basePath . 'detail'),
            'validate' => $this->getTplPath($basePath . 'validate'),
        ];

        return $type ? $stubs[$type] : $stubs;
    }

    /**
     * 设置文件路径
     */
    public function setFilePath()
    {
        $name = $this->getCamelClassName() . 'Validate.' . $this->fileType;
        $this->setCrudFileName($name);

        $module_path = $this->basePath . $this->moduleName . '/validate/';
        if (!empty($this->classDir)) $module_path .= $this->classDir . '/';
        $this->setCrudModuleFilePath($module_path);

        $data_path = $this->crudDir . 'php/app/' . $this->moduleName . '/validate/';
        if (!empty($this->classDir)) $data_path .= $this->classDir . '/';
        $this->setCrudDataFilePath($data_path);
    }

    /**
     * 获取验证器内容
     * @return string|string[]
     */
    public function getValidateContent() {

        $search = [
            '{TABLE_COMMENT}',
            '{CAMEL_CLASS_NAME}',
            '{AUTHOR}',
            '{PK}',
            '{ADD_PARAMS}',
            '{EDIT_PARAMS}',
        ];

        $replace = [
            $this->crudData['table_comment'],
            $this->getCamelClassName(),
            $this->getAuthorContent(),
            $this->getPk(),
            $this->getAddParamsContent(),
            $this->getEditParamsContent()
        ];

        return substr(UtilService::replaceFileContent($search, $replace, $this->getStubContent(TplEnum::getValidateTpl($this->crudData)), false), 0, -1);
    }

    /**
     * 验证规则
     * @return mixed|string
     */
    public function getRuleContent()
    {
        $rule_arr = '';
        $tab2 = UtilService::tab(2);
        foreach ($this->crudColumn as $key => $value) {
            if ($value['is_required'] == 1) {
                $comma = (count($this->crudColumn) - $key) > 1?',':'';
                $rule_arr .= <<<CONTENT
                {$tab2}'$value[column_name]' => 'require'$comma
                CONTENT . PHP_EOL;
            }
        }
        $rule_arr = substr($rule_arr, 0, -1);
        return <<<CONTENT
{$tab2}'{$this->getPk()}' => 'require',
$rule_arr
CONTENT;
    }

    /**
     * 验证字段描述
     * @return string
     */
    public function getMessageContent()
    {
        $message_arr = '';
        $tab2 = UtilService::tab(2);
        foreach ($this->crudColumn as $key => $value) {
            if ($value['is_required'] == 1) {
                $comma = (count($this->crudColumn) - $key) > 1?',':'';
                $message_arr .= <<<CONTENT
                {$tab2}'$value[column_name].require' => '{$this->getTableColumnComment($value['column_comment'])}不能为空'$comma
                CONTENT . PHP_EOL;
            }
        }
        $message_arr = substr($message_arr, 0, -1);
        return <<<CONTENT
{$tab2}'{$this->getPk()}.require' => '{$this->getPk()}不能为空',
$message_arr
CONTENT;
    }

    /**
     * 添加场景验证参数
     * @return string
     */
    public function getAddParamsContent()
    {
        $content = "";
        $is_required = false;
        foreach ($this->crudColumn as $value) {
            if ($value['is_required'] && (!$value['is_pk'])) {
                $is_required = true;
                $content .= "'" . $value['column_name'] . "', ";
            }
        }
        $content = substr($content, 0, $is_required?-2:-1);

        if (!empty($content)) {
            $content = 'return $this->only([' . $content . ']);';
        } else {
            $content = 'return $this->remove(' . "'". $this->getPk() . "'" . ', true);';
        }

        return $content;
    }

    /**
     * 编辑场景验证参数
     * @return string
     */
    public function getEditParamsContent()
    {
        $content = "'" . $this->getPk() . "'," ;
        $is_required = false;
        foreach ($this->crudColumn as $value) {
            if ($value['is_required'] && (!$value['is_pk'])) {
                $is_required = true;
                $content .= "'" . $value['column_name'] . "', ";
            }
        }
        $content = substr($content, 0, $is_required?-2:-1);
        if (!empty($content)) {
            $content = 'return $this->only([' . $content . ']);';
        }
        return $content;
    }

    /**
     * 获取命名空间模板内容
     * @return string
     */
    public function getNameSpaceContent()
    {
        if (!empty($this->classDir)) {
            return "namespace app\\" . $this->moduleName . "\\validate\\" . $this->classDir . ';';
        }
        return "namespace app\\" . $this->moduleName . "\\validate;";
    }

    /**
     * 获取类描述
     * @return string
     */
    public function getTableCommentContent()
    {
        if (!empty($this->crudData['table_comment'])) {
            $tpl = $this->crudData['table_comment'] . '验证器';
        } else {
            $tpl = $this->getCamelClassName() . '验证器';
        }
        return $tpl;
    }

}